package cn.com.dev;

import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.time.Duration;

public class Dev7Moor {
    private static final Log log = LogFactory.getLog(Dev7Moor.class);

    public static void main(String[] args) throws Exception {

        String AccountID = "N00000003082";
        String APISecret = "fd7b96be-b6a1-48b7-a35d-e762008599b7";
        String APIURL = "https://yjwh.tycc100.com/v20180426/callEvent/addCallEvent";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());

        String bBase64 = AccountID + ":" + date;
        String Authorization = KHUtil.getbase64(bBase64);

        // 处理头文件
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type", "application/json");
        headerMap.put("Authorization", Authorization);

        // sig的值为 32位大写MD5加密 （帐号Id + 帐号APISecret +时间戳）
        String bSign = AccountID + APISecret + date;
        String sign = KHUtil.getMD5(bSign).toUpperCase();

        String postURL = APIURL + "/"+AccountID+"?sig=" + sign;
        JSONObject postBody = new JSONObject();
        postBody.put("eventType", "CallEvent");
        postBody.put("concreteType", "RingingURL");
        postBody.put("url", "http://202.69.24.80:8081/ngsapitask/api/api7MoorCallBack");
        postBody.put("name", "RingingURL");
        postBody.put("pbx", "gz.zj.3.3");
        postBody.put("eventToken", "wewewe");
        postBody.put("eventParam", "zapikey:1003,appkey:1111");
        System.out.println("发送请求体：" + postBody.toString());

        // 调用API
        String postResult = Util.doHttpPostUseHead(postURL, postBody.toString(), "UTF-8", 120000, 0, headerMap);
        System.out.println("返回请求体：" + postResult);

    }
}
